<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="com.cms.biz.category.CategoryHelper"%>
<%@page import="com.cms.menu.MenuHelper,net.sf.json.*"%>
<%@page import="java.sql.*, java.util.*, com.cms.config.*, com.cms.facade.*,com.cms.util.*,com.cms.bean.*,com.cms.database.DBHelper"%>
<%@page import="org.apache.log4j.Logger"%>
<%@taglib uri="/i18n-1.0" prefix="i18n"%>
<%
	response.setContentType("text/html; charset=UTF-8");
	SysConfigBean sysbean = ApplicationHelper.getApplicationSysConfigBean(session);
	int categoryTypeId = StringHelper.isRequestParamEmptyInt(request, "categoryTypeId", CategoryTypeDef.NEW);
	int menuId = StringHelper.isRequestParamEmptyInt(request, "menuId", 0);
	Connection conn = DBHelper.getConn();
	List<CategoryBean> list = CategoryHelper.getCategoryListByTypeId(conn, categoryTypeId);
%>
<i18n:bundle baseName="i18n-ebizport" locale="<%=SessionFacade.getLocale(session)%>"/>
<html>
<head>
<meta name="keywords" content="<%=sysbean.getWebKey()%>" />
<meta name="description" content="<%=sysbean.getWebDesc()%>" />
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%=sysbean.getWebName()%></title>
<link rel="stylesheet" href="<%=URLPath.CSS_DIR%>/global.css" type="text/css" media="screen" />
<link rel="stylesheet" href="<%=URLPath.CSS_DIR%>/admin.css" type="text/css" media="screen" />
<script src="<%=URLPath.JS_DIR%>/jquery-1.9.1.js"></script>
<script src="<%=URLPath.JS_DIR%>/main.js"></script>
<script src="<%=URLPath.JS_DIR%>/checkelements.js"></script>
<script src="<%=URLPath.JS_DIR%>/layer.js"></script>
<script type="text/javascript">

$(document).ready(function(){
	var categoryJson = $.parseJSON('<%=JSONArray.fromObject(list)%>');
    $('.addMenu').on('click', function(){
        var parentId = $(this).attr("parentId");
        var parentName = $(this).attr("parentDesc");
    	var addhtml = "<table id='addTypeTable' class='tablist'>"+
        "<tr class='even'><th><i18n:message key='parent'/><i18n:message key='type'/><i18n:message key='name'/>:</th><td>"+parentName+"</td></tr>"+
        "<tr class='odd'><th><i18n:message key='type'/><i18n:message key='name'/>:</th><td><input type='text' id='xxx' class='u-ipt-w300' name='type_name' value=''/></td></tr>"+
        "<tr class='even'><td colspan=2 style='text-align:right'><a href='javascript:save("+parentId+");' class='juanGo'><i18n:message key='ensure'/></a>&nbsp;"+
        "<a href='javascript:cancel();' class='juanLu'><i18n:message key='cancel'/></a></td></tr></table>";

        currlayerObj = createDai('<i18n:message key="add"/><i18n:message key="type"/>',addhtml); 
    });
    
    $('.editMenu').on('click', function(){
        var currId = $(this).attr("currId");
        var currDesc = $(this).attr("currDesc");
        var parentId = $(this).attr("parentId");
    	var edithtml = "<table id='addTypeTable' class='tablist'>"+
        "<tr class='even'><th><i18n:message key='parent'/><i18n:message key='type'/><i18n:message key='name'/>:</th>"+
        "<td><select name='parentId' "+(parentId=='0'?"disabled":"")+"><option value='0'><i18n:message key='nothave'/>";
        for(var i=0;i<categoryJson.length;i++){
        	var catebean = categoryJson[i];
        	edithtml += "<option value='"+catebean.id+"' "+(parentId==catebean.id?"selected":"")+">"+catebean.name;
        }
        edithtml += "</select>&nbsp;*\u53ef\u4ee5\u901a\u8fc7\u7236\u7c7b\u522b\u7684\u9009\u62e9\u8f6c\u79fb\u7c7b\u522b</td>"+
        "</tr>"+
        "<tr class='odd'><th><i18n:message key='type'/><i18n:message key='name'/>:</th><td><input type='text' id='xxx' class='u-ipt-w300' name='type_name' value='"+currDesc+"'/></td></tr>"+
        "<tr class='even'><td colspan=2 style='text-align:right'><a href='javascript:update("+currId+");' class='juanGo'><i18n:message key='ensure'/></a>&nbsp;"+
        "<a href='javascript:cancel();' class='juanLu'><i18n:message key='cancel'/></a></td></tr></table>";

        currlayerObj = createDai('<i18n:message key="edit"/><i18n:message key="type"/>',edithtml); 
    });
    /*$('.delMenu').on('click', function(){
    	var currId = $(this).attr("currId");
    	$("input[value='"+currId+"'][name='setId']").attr("checked",'true');  	
    	deleteSelectRecords("delCategory");
    });*/  
    $(".scroll").css("max-height",$("#main_div",parent.document).height()-180);
    
    initCheckBoxAll();
});

function createDai(title,html){
	var i = $.layer({
    	type: 1,
    	title: true,
    	closeBtn: [0,true],
    	border : [5, 0.5, '#666', true],
    	offset: ['70px',''],
    	move: ['.juanmove', true],
    	area: ['500px','144px'],
    	title : title,
    	page: {
    		html: html
    	}
    });	
	return i;
}

function save(parentId){
	var typeName = $("#addTypeTable input[name|='type_name']").val();
	if(isEmpty(typeName)){
		$.layer({
			area : ['auto','auto'],
			dialog : {msg:'\u540d\u79f0\u4e0d\u80fd\u4e3a\u7a7a!',type : 8}	
		});
		return;
	}
	document.myform.cmd.value = "addCategory";
	document.myform.categoryName.value = typeName;
	document.myform.parentId.value = parentId;
	document.myform.submit();
	cancel();
}
function update(currId){
	var typeName = $("#addTypeTable input[name|='type_name']").val();
	var parentId = $("#addTypeTable select").val();
	if(isEmpty(typeName)){
		layer.alert('\u540d\u79f0\u4e0d\u80fd\u4e3a\u7a7a!', 8);
		return;
	}
	document.myform.cmd.value = "updateCategory";
	document.myform.categoryName.value = typeName;
	document.myform.parentId.value = parentId;
	document.myform.id.value = currId;
	document.myform.submit();
	cancel();
}


</script>
</head>
<body>
<form name="myform" action="<%=URLPath.ADMIN_URL %>"  method="post" target="_self">
<input type="hidden" name="token" value="<%=request.getAttribute("token")%>">
<input type="hidden" name="cmd" value=""/>
<input type="hidden" name="nextCmd" value=""/>
<input type="hidden" name="doNothing" value="N"/>
<input type="hidden" name="id" value="0"/>
<input type="hidden" name="parentId" value="0"/>
<input type="hidden" name="categoryName" value=""/>
<input type="hidden" name="menuId" value="<%=menuId %>"/>
<input type="hidden" name="categoryTypeId" value="<%=categoryTypeId %>"/>
<div class="admin-menu-nav">
   <ul >
     <li style="width:59.9%;"><span class="menu-nav"><i18n:message key="now_position"/>:&nbsp;<%=MenuHelper.getMenuPath(session, menuId) %></span></li>
     <li style="width:40%;">
        <span class="menu-button" onclick="deleteSelectRecords('delCategory','')"><i18n:message key="batch_delete"/></span>
        <span class="menu-button addMenu" parentId="0" parentDesc="<i18n:message key='nothave'/>"><i18n:message key="add"/></span>
     </li>
   </ul>   
</div>
<div class="mainDiv">
<div class="m-fixedheadertable">
	<table>
        <thead>
           <tr>
              <th width="20"><input type="checkbox" name="checkAll" value="" id="checkAll"/></th>
              <th width="84%"><i18n:message key="type"/><i18n:message key="name"/></th>
              <th width="15%"><i18n:message key="option"/></th>
           </tr>
        </thead>
    </table>
	<div class="scroll">
	    <table>
            <tbody>
                <%
                if(list!=null&&list.size()>0){ 
                   for(CategoryBean cBean:list){
                	   List<CategoryBean> subList = cBean.getChildCategoryBeans();
                %>
                <tr>
                   <td width="20"><input type="checkbox" name="setId" value="<%=cBean.getId() %>"/></td>
                   <td width="84%"><%=cBean.getName() %></td>
                   <td width="15%">
                   <a class="addMenu" parentId="<%=cBean.getId() %>" parentDesc="<%=cBean.getName() %>" href="javascript:;"><i18n:message key="add"/></a>&nbsp;
                   <a class="editMenu" parentId='0' currId="<%=cBean.getId() %>" currDesc="<%=cBean.getName() %>" href="javascript:;"><i18n:message key="edit"/></a>&nbsp;
                   </td>
                </tr>
                   <%
                   if(subList!=null&&subList.size()>0){ 
                	   int idx =1;
                	   for(CategoryBean subBean:subList){
                   %> <tr>
                      <td width="20"><input type="checkbox" name="setId" value="<%=subBean.getId() %>"/></td>
                      <td width="84%" style="padding-left:40px;"><em><%=(idx++) %></em>&nbsp;&nbsp;&nbsp;<%=subBean.getName() %></td>
                      <td width="15%">
                      <a class="editMenu" parentId="<%=cBean.getId() %>" currId="<%=subBean.getId() %>" currDesc="<%=subBean.getName() %>"  href="javascript:;"><i18n:message key="edit"/></a>&nbsp;
                      </td>
                      </tr>                   
                   <%} }%>
                
                <%} }%>
            </tbody>
        </table>
    </div>
</div>
</div>
</form>
</body>
</html>
<% DBHelper.closeConn(conn);%>